package com.mycp.privacy.transformers

import com.didiglobal.booster.transform.TransformContext
import com.mycp.privacy.bean.PrivacyBean
import org.objectweb.asm.tree.ClassNode
import kotlin.collections.HashMap

/**
 * @author: liujie
 * @date: 2022/8/23 上午11:27
 * @description: 用于收集隐私注解的转换类
 */
class AnnotationParserClassTransform : InterceptTransformer() {

    companion object{
        //com.mycp.privacy.annotationmark.MethodFix
        const val AsmFieldDesc = "Lcom/mycp/privacy/annotationmark/MethodFix;"
        var asmConfigs = mutableListOf<PrivacyBean>()
        var asmConfigsMap = HashMap<String,String>()
    }

    override fun onPreTransform(context: TransformContext) {
        super.onPreTransform(context)
    }

    override fun onPostTransform(context: TransformContext) {
    }

    @Suppress("SimpleDateFormat")
    override fun transform(context: TransformContext, klass: ClassNode) = klass.also {
//        if ("com/gyzq/uploadlibmaven/privacy/GyPrivacy" == klass.name) {
        if ("com/mycp/videocompress/privacy/GyPrivacy" == klass.name) {
            println("收集注解当前的className="+klass.name)
            klass.methods.forEach { method->
                method.invisibleAnnotations?.forEach { node ->
                    if (node.desc == AsmFieldDesc) {
                        val asmItem = PrivacyBean(klass.name, method, node)
                        if (!asmConfigs.contains(asmItem)) {
                            asmConfigs.add(asmItem)
                            asmConfigsMap[klass.name] = klass.name
                        }
                    }
                }
            }
        }
    }
}